package com.xxxx.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxxx.server.pojo.*;
import com.xxxx.server.service.INoticeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2022-06-26
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService iNoticeService;

    @ApiOperation(value = "查看所有通知")
    @GetMapping("/getAllNotice/")
    public List<Notice> getAllNotice(){
        return iNoticeService.list();
    }

    //type=1 是通知 =2是小贴士
    @ApiOperation(value = "查看通知type=1")
    @GetMapping("/getTongZhiNotice/")
    public List<Notice> getTongZhiNotice(){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",1);
        return iNoticeService.list(queryWrapper);
    }

    @ApiOperation(value = "查看通知type=2")
    @GetMapping("/getTipsNotice/")
    public List<Notice> getTipsNotice(){
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type",2);
        return iNoticeService.list(queryWrapper);
    }

    @ApiOperation(value = "查询所有的通知（分页）")
    @GetMapping("/getPageNoties/")
    public RespPageBean getPageNoties(@RequestParam(defaultValue = "1") Integer currentPage ,
                                          @RequestParam(defaultValue = "10")Integer size,
                                          Notice notice,
                                          String[] beginDateScope){
        return iNoticeService.getPageNoties(currentPage,size,notice,beginDateScope);
    }

    @ApiOperation(value = "添加通知信息")
    @PostMapping("/add")
    public RespBean addNotice(@RequestBody Notice notice){
        notice.setPerson(((Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName());
        notice.setJinyong(0);
        notice.setStime(LocalDateTime.now());
        notice.setNum(0);

        if (iNoticeService.save(notice)){
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "更新通知")
    @PutMapping("/update")
    public RespBean updateNotice(@RequestBody Notice notice){
        notice.setXtime(LocalDateTime.now());
        if (iNoticeService.updateById(notice)){
            return RespBean.success("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @ApiOperation(value = "删除通知信息")
    @DeleteMapping("/deleteOne/{id}")
    public RespBean deleteNotice(@PathVariable Integer id){
        if (iNoticeService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    @ApiOperation(value = "批量删除通知信息")
    @DeleteMapping("/")
    public RespBean deleteNoticeByIds(Integer[] ids){
        if (iNoticeService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

}
